# !/usr/bin/env python
import datetime

from django.db.models import Count


def set_env():
    import os
    import sys
    import django

    project_path = os.path.abspath(os.path.realpath(__file__) + r"/../../../")

    # 添加工程名
    sys.path.append(project_path)
    # 工程路径
    os.chdir(project_path)
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Box.settings.develop")
    django.setup()


def test():
    set_env()

    from system.models import UserProfile, Router, Server, Terminal, Line, ApplyAccount
    import datetime

    # today = datetime.datetime.now().day
    # yesterday = (datetime.datetime.now() - datetime.timedelta(days=1)).day
    # today_nums = UserProfile.objects.filter(date_joined__day=today).count()
    # yesterday_nums = UserProfile.objects.filter(date_joined__day=yesterday).count()
    # print(today_nums, yesterday_nums)
    #
    # print(today, yesterday)
    #
    # start = datetime.datetime.now() - datetime.timedelta(hours=23, minutes=59, seconds=59)
    # print(UserProfile.objects.filter(date_joined__day=4), start)

    # now = datetime.datetime.now()
    # yesterday = now - datetime.timedelta(hours=23, minutes=59, seconds=59)
    # user_nums = UserProfile.objects.filter(date_joined__gte=yesterday)
    #
    # print(now, yesterday)
    # print(user_nums)

    # 查询时间段
    # start_date = datetime.date(2020, 4, 20)
    # end_date = datetime.date(2020, 5, 4)
    # user_nums = UserProfile.objects.filter(date_joined__range=(start_date, end_date)).count()
    # print(user_nums)
    #
    # # 查询某年
    # UserProfile.objects.filter(date_joined__year=2020)
    # UserProfile.objects.filter(date_joined__month=5)
    # UserProfile.objects.filter(date_joined__day=4)
    # UserProfile.objects.filter(date_joined__week_day=2)

    results = Line.objects.values('province', 'city').annotate(nums=Count('province')).values('province',
                                                                                              'city', 'nums')
    print(results)

    account = ApplyAccount.objects.filter(user_id=3)[0]

    apply_time = account.apply_time
    expire_time = account.expire_time
    print((expire_time - apply_time).days)

    params = {
        'name': 'aaua',
        'client_type': '通用客户端',
        'nums': '1',
        'enable_lines': "北京北京,广东广州,广东深圳".split(','),
        'operator': 2,
        'ip_change': 0,
        'expire_time': '2020-11-05',
        'user_id': 3
    }

    ApplyAccount.objects.create(**params)


if __name__ == '__main__':
    test()
